﻿Imports System.IO

Public Class FormMain

    Public ReadOnly Property SelectedIndex() As Integer
        Get
            Return lvwProfiles.SelectedIndices(0)
        End Get
    End Property

    Public ReadOnly Property ProfileName() As String
        Get
            Return lvwProfiles.Items(Me.SelectedIndex).Text
        End Get
    End Property


    Sub NewProfile()
        Dim dlgProfile As New FormProfile
        dlgProfile.Profile = Nothing

        If dlgProfile.ShowDialog = Windows.Forms.DialogResult.OK Then
            ' Save profile to file.
            FileHandler.SaveProfile(dlgProfile.Profile)

            ' Add Icon to ImageLists.
            imgLarge.Images.Add(dlgProfile.Profile.Name, dlgProfile.Profile.Icon)
            imgSmall.Images.Add(dlgProfile.Profile.Name, dlgProfile.Profile.Icon)

            ' Add profile to list view.
            Dim lvi As New ListViewItem
            lvi.Name = dlgProfile.Profile.Name
            lvi.ImageKey = dlgProfile.Profile.Name
            lvi.Text = dlgProfile.Profile.Name
            lvwProfiles.Items.Add(lvi)
        End If
    End Sub

    Sub EditProfile()
        Dim dlgProfile As New FormProfile

        If dlgProfile.ShowDialog(ProfileName) = Windows.Forms.DialogResult.OK Then
            ' Save profile to file.
            FileHandler.SaveProfile(dlgProfile.Profile)

            ' Add Icon to ImageLists.
            Dim key As String = dlgProfile.Profile.Name
            imgLarge.Images.RemoveByKey(key)
            imgSmall.Images.RemoveByKey(key)
            imgLarge.Images.Add(key, dlgProfile.Profile.Icon)
            imgSmall.Images.Add(key, dlgProfile.Profile.Icon)

            ' Add profile to list view.
            Dim lvi As New ListViewItem
            lvi.Name = key
            lvi.ImageKey = key
            lvi.Text = key
            lvwProfiles.Items(SelectedIndex) = lvi
        End If
    End Sub

    Sub DeleteProfile()
        If MsgBox("Are you sure you want to delete this profile?", MsgBoxStyle.YesNo, "Delete " & ProfileName) = MsgBoxResult.Yes Then
            If FileHandler.DeleteProfile(ProfileName) Then
                lvwProfiles.Items.RemoveAt(SelectedIndex)
            Else
                MsgBox("There was a problem while deleting the profile.")
            End If
        End If
    End Sub

    Sub PopulateListView()
        lvwProfiles.Items.Clear()
        imgLarge.Images.Clear()
        imgSmall.Images.Clear()
        Dim lvi As ListViewItem = Nothing
        Dim p As GameProfile = Nothing
        For Each profileName As String In FileHandler.GetProfileNames()
            p = FileHandler.LoadProfile(profileName)
            ' Add Icon to ImageList(s).
            imgLarge.Images.Add(p.Name, p.Icon)
            imgSmall.Images.Add(p.Name, p.Icon)
            ' Add profile to ListView
            lvi = New ListViewItem
            lvi.ImageKey = p.Name
            lvi.Name = p.Name
            lvi.Text = p.Name
            lvwProfiles.Items.Add(lvi)
        Next profileName
    End Sub



    Private Sub FormMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.PopulateListView()
        tsbEdit.Enabled = False
        tsbDelete.Enabled = False
        tsbRun.Enabled = False
        chName.Width = lvwProfiles.ClientSize.Width

        'HACK: Details view has been disabled for now since there's no real need without details to view.
        tsbDetailsView.Visible = False

        Dim options As New ApplicationSettings
        FileHandler.LoadOptions(options)
        If Not File.Exists(options.PsxfinExePath) Then
            Dim dlgOptions As New FormOptions
            dlgOptions.ShowDialog()
        End If

        Me.Location = options.FormMainLocation
        Me.Size = options.FormMainSize
        lvwProfiles.View = options.FormMainViewMode
        lvwProfiles_SelectedIndexChanged(Nothing, Nothing)
        Select Case lvwProfiles.View
            Case View.Details : tsbDetailsView.Checked = True
            Case View.LargeIcon : tsbLargeIconView.Checked = True
            Case View.List : tsbListView.Checked = True
        End Select
    End Sub

    Private Sub FormMain_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Dim options As New ApplicationSettings
        FileHandler.LoadOptions(options)
        options.FormMainViewMode = lvwProfiles.View
        options.FormMainLocation = Me.Location
        options.FormMainSize = Me.Size
        FileHandler.SaveOptions(options)
    End Sub


    Private Sub mnuOptions_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuOptions.Click
        Dim dlgOptions As New FormOptions
        dlgOptions.ShowDialog()
    End Sub

    Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
        Me.Close()
    End Sub

    Private Sub cmiNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmiNew.Click
        Me.NewProfile()
    End Sub

    Private Sub cmiEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmiEdit.Click
        Me.EditProfile()
    End Sub

    Private Sub cmiDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmiDelete.Click
        Me.DeleteProfile()
    End Sub


    Private Sub tsbNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbNew.Click
        Me.NewProfile()
    End Sub

    Private Sub tsbEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbEdit.Click
        Me.EditProfile()
    End Sub

    Private Sub tsbDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbDelete.Click
        Me.DeleteProfile()
    End Sub

    Private Sub tsbRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbRun.Click
        FileHandler.RunProfile(ProfileName)
    End Sub

    Private Sub ViewChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbDetailsView.Click, tsbLargeIconView.Click, tsbListView.Click
        Select Case TryCast(sender, ToolStripButton).Text
            Case "Details View"
                tsbDetailsView.Checked = True
                tsbListView.Checked = False
                tsbLargeIconView.Checked = False
                lvwProfiles.View = View.Details
                chName.Width = lvwProfiles.ClientSize.Width
            Case "List View"
                tsbDetailsView.Checked = False
                tsbListView.Checked = True
                tsbLargeIconView.Checked = False
                lvwProfiles.View = View.List
            Case "Large Icon View"
                tsbDetailsView.Checked = False
                tsbListView.Checked = False
                tsbLargeIconView.Checked = True
                lvwProfiles.View = View.LargeIcon
            Case Else
                'Do Nothing
        End Select
    End Sub


    Private Sub lvwProfiles_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvwProfiles.SelectedIndexChanged
        If lvwProfiles.SelectedIndices.Count = 0 Then
            tsbEdit.Enabled = False
            tsbDelete.Enabled = False
            tsbRun.Enabled = False
            tsbRun.Text = "Run"
            cmiEdit.Visible = False
            cmiDelete.Visible = False
        Else
            tsbEdit.Enabled = True
            tsbDelete.Enabled = True
            tsbRun.Enabled = True
            tsbRun.Text = "Run " & ProfileName
            cmiEdit.Visible = True
            cmiDelete.Visible = True
        End If
    End Sub

    Private Sub lvwProfiles_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvwProfiles.DoubleClick
        If lvwProfiles.SelectedItems.Count > 0 Then
            FileHandler.RunProfile(ProfileName)
        End If
    End Sub

End Class
